﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using InstituteManager.Bussiness;
using InstituteManager.Data;
using System.Security.Cryptography;
using System.IO;
using System.Web.Security;

namespace InstituteManager.adm
{
    public partial class changepassword : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["Username"] != null)
            switch (Session["Permission"].ToString())
            {
                 case "0":
                    pnlStaff.Visible = true;
                    Data.tbl_Student obj1 = new tbl_Student();
                    obj1 = StudentBL.getByUsername(Session["Username"].ToString());
                    lblStaffName.Text = obj1.StudentName;
                    lblDOB.Text = obj1.DOB.Value.ToString("d");
                    lblAddress.Text = obj1.Address;
                    lblPhone.Text = obj1.StudentPhone;                    
                    lblUsername.Text = obj1.Username;
                    lblEmail.Text = obj1.Email;

                    lblPermission.Text = "Student";
                    break;
                case "1":
                case "2":
                case "3":
                    pnlStaff.Visible = true;
                    Data.tbl_Staff obj = new tbl_Staff();
                    obj = StaffBL.getByUsername(Session["Username"].ToString());
                    lblStaffName.Text = obj.StaffName;
                    lblAddress.Text = obj.Address;
                    lblPhone.Text = obj.Phone;
                    lblPermission.Text = getPermissionName(obj.Permission.ToString());
                    lblUsername.Text = obj.Username;
                    lblEmail.Text = obj.Email;
                    break;                
            }
        }
        protected string getPermissionName(string Permission)
        {
            switch (Permission)
            {
                case "1":
                    return "Administrator";
                case "2":
                    return "Manager";
                case "3":
                    return "Teacher";
            }

            return "unknown";
        }

        protected void ShowMessage(int MegType, string Message)
        {
            pnlMessage.Visible = true;
            ((Label)pnlMessage.FindControl("lblMessage")).Text = Message;
            switch (MegType)
            {
                case 1:
                    pnlMessage.CssClass = "sukses";
                    break;
                case 0:
                    pnlMessage.CssClass = "gagal";
                    break;
                default:
                    pnlMessage.CssClass = "informasi";
                    break;
            }
        }

        protected void lbtChangePass_Click(object sender, EventArgs e)
        {
            string MD5Pass = FormsAuthentication.HashPasswordForStoringInConfigFile(txtNewPassword.Text, "MD5");
            if (Session["Username"] != null && Session["Username"].ToString() == "0")
                if (StudentBL.changePassword(Session["Username"].ToString(), MD5Pass))
                    ShowMessage(1, "Password changed successfully !");
                else
                    ShowMessage(0, "Password changed fail !");
            else
                if (StaffBL.changePassword(Session["Username"].ToString(), MD5Pass))
                    ShowMessage(1, "Password changed successfully !");
                else
                    ShowMessage(0, "Password changed fail !");

        }

        
    }
}